package chp14;

import chp10.Student;

import java.util.Scanner;

/**
 * 学生管理系统
 * 负责菜单的输入输入输出 和 学生的信息保持
 */
public class StudentManager {

    //344 34s fsd null null....
    Student[] students = new Student[10];

    Scanner input = new Scanner(System.in);

    /**
     * 初始化
     */
    public void init(){
        students[0] = new Student();
        students[0].id = 101;
        students[0].name = "jack";
        students[0].age = 19;

        students[1] = new Student();
        students[1].id = 105;
        students[1].name = "tom";
        students[1].age = 22;

        students[2] = new Student();
        students[2].id = 109;
        students[2].name = "rose";
        students[2].age = 17;
        System.out.println("学生信息初始化成功");

    }

    /**
     * 显示主菜单
     */
    public void showMainMenu(){
        while (true) {
            System.out.println("\n1.查询全部学生信息");
            System.out.println("2.新增学生信息");
            System.out.println("3.修改学生信息");
            System.out.println("4.删除学生信息");
            System.out.println("0.退出");
            System.out.println("请选择:");
            String choose = input.next();
            if(choose.equals("0")){
                break;
            }else if(choose.equals("1")){
                showAllStudent();
            }else if(choose.equals("2")){
                addStudent();
            }else if(choose.equals("3")){
                updateStudent();
            }else if(choose.equals("4")){

            }
        }
    }

    public void updateStudent(){
        System.out.print("请输入学号:");
        int id = input.nextInt();
        //找出id对应的学生
        Student student = getStudentById(id);
        if(student == null){
            System.out.println("该学生信息不存在");
            return;
        }

        System.out.println("学号:"+id+" 的学生 " + student.name+" 年龄:"+student.age);
        System.out.println("请输入新的年龄:");
        int age = input.nextInt();

        System.out.print("确认要把年龄从" + student.age +" 岁改为" +age+"岁吗？(y/n):");
        String answer = input.next();
        if(answer.equals("y")){
            // 为什么这里该对象的值，最终可以影响到数组里面的信息
            // 关键点:引用
            student.age = age;
            System.out.println("修改成功");
        }

    }

    public void addStudent(){
        int id = 0;
        while (true) {
            System.out.print("请输入学号:");
            id = input.nextInt();
            if (getStudentById(id) != null) {
                System.out.println("该学号已经存在了，请重新输入");
                continue;
            }
            break;
        }

        System.out.print("请输入姓名:");
        String name = input.next();
        System.out.print("请输入年龄:");
        int age = input.nextInt();

        //创建一个新的学生对象
        Student student = new Student();
        student.id = id;
        student.age = age;
        student.name = name;

        //存入数组合适位置
        for (int i = 0; i < students.length; i++) {
            if(students[i] == null){
                students[i] = student;
                System.out.println("添加成功");
                break;
            }
        }
    }


    /**
     * 根据id查询学生
     * @param id 学号
     * @return 学生对象 如果找不到，则返回null
     */
    public Student getStudentById(int id){
        Student student = null;
        for (int i = 0; i < students.length; i++) {
            if(students[i] == null){
                break;
            }
            if(students[i].id == id){
                student = students[i];
            }
        }
        return student;
    }

    /**
     * 显示所有学生信息
     * 学号 姓名 年龄
     */
    public void showAllStudent(){
        System.out.println("学号 姓名 年龄");
        for (int i = 0; i < students.length; i++) {
            if(students[i] == null){
                break;
            }
            System.out.print(students[i].id + " ");
            System.out.print(students[i].name + " ");
            System.out.println(students[i].age);
        }
    }
}